﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ENail.Domain.Core;
using ENail.Domain.Core.Contract.Services;
using Equation.Enterprise.Framework.Domain.Data;
using Equation.Enterprise.Utilities.DesignByContract;

namespace ENail.ApplicationServices
{
    public class ReceiptService : IReceiptService
    {
        private readonly IRepository<Receipt> _receiptRepository;

        public ReceiptService(IRepository<Receipt> nailServiceRepository)
        {
            Check.Require(nailServiceRepository != null);

            this._receiptRepository = nailServiceRepository;
        }

        /// <summary>
        /// Helps view all receipts on list view
        /// </summary>
        /// <returns>List of all receipts</returns>
        public IList<Receipt> GetAllReceipts()
        {
            var nailServices = _receiptRepository.List();

            Check.Ensure(nailServices != null);

            return nailServices.ToList();
        }

        /// <summary>
        /// Helps view receipts for search view
        /// </summary>
        /// <param name="where">Lambda condition to get receipts</param>
        /// <returns></returns>
        public IList<Receipt> GetReceipts(Expression<Func<Receipt, bool>> @where)
        {
            var nailServices = _receiptRepository.List(@where);

            Check.Ensure(nailServices != null);

            return nailServices.ToList();
        }

        /// <summary>
        /// Helps view active receipts only
        /// </summary>
        /// <returns>All active receipts</returns>
        public IList<Receipt> GetAllActiveReceipts()
        {
            var nailServices = _receiptRepository.List(x => x.IsActive);

            Check.Ensure(nailServices != null);

            return nailServices.ToList();
        }

        /// <summary>
        /// Helps view details of an receipt
        /// </summary>
        /// <param name="id">Identity of receipt</param>
        /// <returns>Receipt with specified identity</returns>
        public Receipt GetReceipt(int id)
        {
            Check.Require(id > 0);

            Receipt nailService = _receiptRepository.Get(id);

            Check.Ensure(nailService != null);

            return nailService;
        }

        /// <summary>
        /// Insert an receipt from view
        /// </summary>
        /// <param name="receipt">Receipt to be inserted</param>
        /// <returns>Number of records affected</returns>
        public int InsertReceipt(Receipt nailService)
        {
            Check.Require(nailService != null);

            _receiptRepository.Add(nailService);
            return _receiptRepository.SaveChanges();
        }

        /// <summary>
        /// Update an receipt from view
        /// </summary>
        /// <param name="receipt">Receipt to be updated</param>
        /// <returns>Number of records affected</returns>
        public int UpdateReceipt(Receipt nailService)
        {
            Check.Require(nailService != null);

            _receiptRepository.Update(nailService);
            return _receiptRepository.SaveChanges();
        }

        /// <summary>
        /// Helps to inactive an receipt
        /// </summary>
        /// <param name="receipt">Receipt to be inactive</param>
        /// <returns>Number of records affected</returns>
        public int DeActivateReceipt(Receipt nailService)
        {
            Check.Require(nailService != null);

            nailService.IsActive = false;
            _receiptRepository.Update(nailService);
            return _receiptRepository.SaveChanges();
        }
    }
}
